package com.cui.project.config;


import com.api.common.constant.APIInterfaceMqConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqBeanConfig {

    // ==================== 1. 接口日志队列 ====================

    @Bean
    public Queue interfaceLogQueue() {
        return new Queue(
                APIInterfaceMqConstant.API_INTERFACE_LOG_QUEUE,
                true,  // durable: 持久化
                false, // exclusive: 非独占
                false  // autoDelete: 不自动删除
        );
    }

    @Bean
    public DirectExchange interfaceLogExchange() {
        return new DirectExchange(
                APIInterfaceMqConstant.API_INTERFACE_LOG_EXCHANGE,
                true,  // durable: 持久化
                false  // autoDelete: 不自动删除
        );
    }

    @Bean
    public Binding interfaceLogBinding(Queue interfaceLogQueue,
                                       DirectExchange interfaceLogExchange) {
        return BindingBuilder
                .bind(interfaceLogQueue)
                .to(interfaceLogExchange)
                .with(APIInterfaceMqConstant.API_INTERFACE_LOG_ROUTE_KEY);
    }

    // ==================== 2. 动态路由队列 ====================

    @Bean
    public Queue dynamicRouteQueue() {
        return new Queue(
                APIInterfaceMqConstant.API_DYNAMIC_ROUTE_QUEUE,
                true,
                false,
                false
        );
    }

    @Bean
    public DirectExchange dynamicRouteExchange() {
        return new DirectExchange(
                APIInterfaceMqConstant.API_DYNAMIC_ROUTE_EXCHANGE,
                true,
                false
        );
    }

    @Bean
    public Binding dynamicRouteBinding(Queue dynamicRouteQueue,
                                       DirectExchange dynamicRouteExchange) {
        return BindingBuilder
                .bind(dynamicRouteQueue)
                .to(dynamicRouteExchange)
                .with(APIInterfaceMqConstant.API_DYNAMIC_ROUTE_ROUTE_KEY);
    }

    // ==================== 3. 网关刷新队列 ====================

    @Bean
    public Queue dynamicRefreshRouteQueue() {
        return new Queue(
                APIInterfaceMqConstant.API_DYNAMIC_REFRESH_ROUTE_QUEUE,
                true,
                false,
                false
        );
    }

    /**
     * 网关刷新队列绑定到动态路由交换机（复用）
     */
    @Bean
    public Binding dynamicRefreshRouteBinding(Queue dynamicRefreshRouteQueue,
                                              DirectExchange dynamicRouteExchange) {
        return BindingBuilder
                .bind(dynamicRefreshRouteQueue)
                .to(dynamicRouteExchange)
                .with(APIInterfaceMqConstant.API_DYNAMIC_REFRESH_ROUTE_ROUTE_KEY);
    }

    // ==================== 4. 网关访问日志队列 ====================

    @Bean
    public Queue gatewayAccessLogQueue() {
        return new Queue(
                APIInterfaceMqConstant.API_GATEWAY_ACCESS_LOG_QUEUE,
                true,
                false,
                false
        );
    }

    @Bean
    public DirectExchange gatewayAccessLogExchange() {
        return new DirectExchange(
                APIInterfaceMqConstant.API_GATEWAY_ACCESS_LOG_EXCHANGE,
                true,
                false
        );
    }

    @Bean
    public Binding gatewayAccessLogBinding(Queue gatewayAccessLogQueue,
                                           DirectExchange gatewayAccessLogExchange) {
        return BindingBuilder
                .bind(gatewayAccessLogQueue)
                .to(gatewayAccessLogExchange)
                .with(APIInterfaceMqConstant.API_GATEWAY_ACCESS_LOG_ROUTE_KEY);
    }

    // ==================== 5. 接口调用次数补偿队列 ====================

    @Bean
    public Queue invokeCountCompensationQueue() {
        return new Queue(
                APIInterfaceMqConstant.API_INVOKE_COUNT_COMPENSATION_QUEUE,
                true,  // durable: 持久化
                false, // exclusive: 非独占
                false  // autoDelete: 不自动删除
        );
    }

    @Bean
    public DirectExchange invokeCountCompensationExchange() {
        return new DirectExchange(
                APIInterfaceMqConstant.API_INVOKE_COUNT_COMPENSATION_EXCHANGE,
                true,  // durable: 持久化
                false  // autoDelete: 不自动删除
        );
    }

    @Bean
    public Binding invokeCountCompensationBinding(Queue invokeCountCompensationQueue,
                                                  DirectExchange invokeCountCompensationExchange) {
        return BindingBuilder
                .bind(invokeCountCompensationQueue)
                .to(invokeCountCompensationExchange)
                .with(APIInterfaceMqConstant.API_INVOKE_COUNT_COMPENSATION_ROUTE_KEY);
    }
}
